package cn.wgx.module.mind.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.mind.MindBeatContent;
import cn.wgx.common.entity.mind.MindBeatContentHis;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.mind.entity.mapper.BeatContentMapper;
import cn.wgx.module.mind.service.BeatContentHisService;
import cn.wgx.module.mind.service.BeatContentService;
import cn.wgx.module.mind.websocket.util.SWLockUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "05.1 节拍点正文")
@RestController
//@RefreshScope
@RequestMapping("/api/mind/beatContent")
public class BeatContentCtrl extends IBaseController<BeatContentService, MindBeatContent> {

    @Autowired
    private BeatContentHisService beatContentHisService;


    @ApiOperation(value = "查询, 根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("list")
    public Map list(@ModelAttribute MindBeatContent t,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.page(page1, new QueryWrapper<>(t)).getRecords());
    }

    @ApiOperation(value = "查询, 根据ID list查询,")
    @GetMapping("listByIds")
    public Map listByIds(@RequestParam List<Integer> id) {
        if(id.isEmpty()){
            ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
        }
        QueryWrapper<MindBeatContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("beat_id", id);
        return ResponseDataUtil.returnData(service.list(queryWrapper));
    }

    @ApiOperation(value = "查询, 根据主键ID查询单条数据")
    @GetMapping("get")
    public Map getOne(@RequestParam Integer id) {
        return ResponseDataUtil.returnData(service.getById(id));
    }




    @ApiOperation(value = "更新, 根据主键ID修改实体数据,可局部修改(只提交需要修改的数据)")
    @PatchMapping("update")
    public Map update(@RequestBody MindBeatContent t) {
        if(!SWLockUtil.canUpdate(t.getRandomName(), service.getProIdById(t.getBeat_id()), t.getBeat_id())){
            return ResponseDataUtil.returnErrorMap("其他用户正在编辑此节点内容.");
        }
        return ResponseDataUtil.returnData(service.updateById(t));
    }



    @ApiOperation(value = "历史内容查询, 根据beat_id查询历史保存内容")
    @GetMapping("listHisById")
    public Map listHisById(@RequestParam Integer beat_id,
                           @RequestParam(required = false, defaultValue = "1") Integer page,
                           @RequestParam(required = false, defaultValue = "20") Integer limit) {
        if(beat_id.equals(0)){
            ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
        }
        return ResponseDataUtil.returnData(beatContentHisService.listByBeatId(new Page<>(page, limit), beat_id));
    }

    @ApiOperation(value = "历史数据替换成当前内容")
    @PatchMapping("updateByHis")
    public Map apply(@ApiParam("节点ID") @RequestParam Integer beat_id,
                     @ApiParam("历史数据ID") @RequestParam Integer beat_his_id) {
        return ResponseDataUtil.returnData(service.applyHis(beat_id, beat_his_id));
    }

//------ 以下方法不暴露


    @ApiIgnore()
    @Override
    public Map updateBatch(@RequestBody List<MindBeatContent> t) {
        return ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
    }


    @ApiIgnore()
    public Map del(@PathVariable Integer id) {
        return ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
    }


    @ApiIgnore()
    @Override
    public Map delBatch(@RequestBody List<Integer> ids) {
        return ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
    }

    @ApiIgnore()
    @Override
    public Map add(@Validated @RequestBody MindBeatContent t) {
        return ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
    }


    @ApiIgnore()
    @Override
    public Map addBatch(@Validated @RequestBody List<MindBeatContent> t) {
        return ResponseDataUtil.returnData(ResponseDataUtil.EMPTY_LIST);
    }
}
